package com.wcity.manage.web;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Role;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.RoleService;
import com.wcity.manage.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户管理控制层
 *
 * @author dy
 */
@Controller
public class UserAct {

    /**
     * 用户列表
     *
     * @param user
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping( "user_list" )
    public String userList(User user, PageModel pageModel, HttpServletRequest request ) {
        pageModel.setParams( user );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "user", user );
        request.setAttribute( "list", userService.getUserListPage( pageModel ) );
        return "manage/userlist";
    }

    /**
     * 跳转至添加用户页面
     *
     * @param request
     * @return
     */
    @RequestMapping( "to_user_add" )
    public String toUserAdd( HttpServletRequest request ) {
        request.setAttribute( "roleList", roleService.getRoleList( new Role() ) );
        return "manage/useradd";
    }

    /**
     * 添加用户
     *
     * @param user
     * @param response
     */
    @RequestMapping( "user_add" )
    public void userAdd( User user, HttpServletResponse response ) {

        String errorMsg = userService.addUser( user );
        if ( StringUtil.isBlank( errorMsg ) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "user_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 跳转至编辑用户页面
     *
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping( "to_user_edit" )
    public String  toUserEdit( int id, HttpServletRequest request, HttpServletResponse response ) {
        User user = userService.getUser( id );
        if ( user == null ) {
            return "manage/useredit";
        }
        user.setId( id );
        request.setAttribute( "user", user );
        request.setAttribute( "roleList", roleService.getRoleList( new Role() ) );
        return "manage/useredit";
    }

    /**
     * 编辑用户
     *
     * @param id
     * @param user
     * @param request
     * @param response
     */
    @RequestMapping( "user_edit" )
    public void userEdit( int id, User user, HttpServletRequest request, HttpServletResponse response ) {
        String errorMsg = userService.updateUser( user, id );
        if ( StringUtil.isBlank( errorMsg ) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "更新成功", "user_list", "", "closeCurrent", "" ) );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 跳转至用户信息界面
     *
     * @param request
     * @return
     */
    @RequestMapping( "user_info" )
    public String userInfo( HttpServletRequest request ) {
        request.setAttribute( "user", request.getSession().getAttribute( SessionKey.SESSION_USER_INFO ) );
        return "manage/userinfo";
    }

    /**
     * 跳转至修改密码页面
     *
     * @param request
     * @return
     */
    @RequestMapping( "to_edit_user_info" )
    public String toEditPwd( HttpServletRequest request ) {
        return "manage/userinfoedit";
    }

    /**
     * 用户修改信息
     *
     * @param oripwd
     * @param user
     * @param request
     * @param response
     */
    @RequestMapping( "edit_user_info" )
     public void editUserInfo( String oripwd, User user, HttpServletRequest request, HttpServletResponse response ) {

        User localUser = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        String errorMsg = userService.editUserInfo(localUser, oripwd, user );
        if ( StringUtil.isBlank( errorMsg ) ) {
            // 添加成功
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson("200", "更新成功", "user_list", "", "closeCurrent", "") );
        } else {
            // 添加失败
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    @Resource private UserService userService;
    @Resource private RoleService roleService;

}
